//
//  AlertViewDJ.m
//  MatrixProject
//
//  Created by 陈德军 on 2017/4/21.
//  Copyright © 2017年 ChenDJ. All rights reserved.
//

#import "AlertViewDJ.h"
#import "AppDelegate.h"



#define HEXColor(hexValue,alphaValue) [UIColor colorWithRed:((float)((hexValue & 0xFF0000) >> 16))/255.0 green:((float)((hexValue & 0xFF00) >> 8))/255.0 blue:((float)(hexValue & 0xFF))/255.0 alpha:alphaValue]
#define autoScaleW(width) [(AppDelegate *)[UIApplication sharedApplication].delegate autoScaleW:width]
#define autoScaleH(height) [(AppDelegate *)[UIApplication sharedApplication].delegate autoScaleH:height]

@interface AlertViewDJ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>{
    
}
@property (nonatomic,strong) UITableView *table;
@property (nonatomic,strong) NSArray *data;
@property (nonatomic,strong) NSMutableDictionary *dic;
@property (nonatomic,strong) UIWindow * backWindow;
@property (nonatomic,strong ) UIView *selctview;
@property (nonatomic,assign) BOOL sec;
@end


@implementation AlertViewDJ

- (UIWindow*)backWindow{
    if (!_backWindow) {
        _backWindow=[[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
        _backWindow.windowLevel=UIWindowLevelAlert;
        [_backWindow becomeKeyWindow];
        [_backWindow makeKeyAndVisible];
    }
    return _backWindow;
}

- (instancetype)init{
    self = [super init];
    self.frame = [UIScreen mainScreen].bounds;
    
    [self setBackgroundColor:[UIColor blackColor]];
    self.backgroundColor = [UIColor clearColor];
    return self;
}


- (void)setlist:(NSArray *)data{
    CGFloat h = data.count *60;
    _data = data;
    if (h > 300) {
        h = 300;
    }
    _table = [[UITableView alloc]initWithFrame:CGRectMake(30, 20, kScreenWidth-60, h) style:(UITableViewStylePlain)];
    _table.centerY = self.centerY;
    _table.backgroundColor = [UIColor whiteColor];
    _table.delegate = self;
    _table.dataSource = self;
    _table.layer.cornerRadius = 5;
    _table.separatorStyle =   UITableViewCellSeparatorStyleNone;
    _table.rowHeight = 60;
    [self addSubview:_table];
    _backWindow=[[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    _backWindow.windowLevel=UIWindowLevelAlert;
    [_backWindow becomeKeyWindow];
    [_backWindow makeKeyAndVisible];
    _selctview = _table;
    //[_backWindow addSubview:self];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _data.count;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *ider = [NSString stringWithFormat:@"mycell%ld",indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ider];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:ider];
    }else{
        return cell;
    }
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, kScreenWidth-60, 40)];
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:14];
    label.textColor = [UIColor redColor];
    label.text = _data[indexPath.row];
    [cell.contentView addSubview:label];
    UIImageView * pic = [[UIImageView alloc]initWithFrame:CGRectMake(0, 59, kScreenWidth-60, 1)];
    pic.image = [UIImage imageNamed:@"shuip"];
    UIImageView * pic2 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 59, kScreenWidth-60, 1)];
    pic2.image = [UIImage imageNamed:@"shuip"];
    [cell.contentView addSubview:pic];
    [cell setSelectedBackgroundView:pic2];
    return cell;
}

- (void)show{
    [self.backWindow addSubview:self];
    [self layerAnimationMakeWithUp:YES];
    CGPoint startPoint = CGPointMake(self.center.x, -_selctview.frame.size.height);
    _selctview.layer.position=startPoint;
    
    //damping:阻尼，范围0-1，阻尼越接近于0，弹性效果越明显
    //velocity:弹性复位的速度
    [UIView animateWithDuration:.8 delay:0 usingSpringWithDamping:.6 initialSpringVelocity:1.0 options:UIViewAnimationOptionCurveLinear animations:^{
        _selctview.layer.position=self.center;
        
    } completion:^(BOOL finished) {
        
    }];
    
}
- (void)dismissAlertView{
    [self layerAnimationMakeWithUp:NO];
    [UIView animateWithDuration:0.15 animations:^{
        
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        [_backWindow resignKeyWindow];
        _backWindow.hidden = YES;
        
    }];
    
}

-(void)layerAnimationMakeWithUp:(BOOL)up{
    [self.layer removeAllAnimations];
    CABasicAnimation *colorAnimation = [CABasicAnimation animationWithKeyPath:@"backgroundColor"];
    
    if (up == YES) {
        colorAnimation.duration = 0.3;
        colorAnimation.fromValue = (__bridge id _Nullable)([HEXColor(0x000000, 0) CGColor]);
        colorAnimation.toValue = (__bridge id _Nullable)([HEXColor(0x000000, 0.4) CGColor]);
    }else{
        colorAnimation.duration = 0.15;
        colorAnimation.fromValue = (__bridge id _Nullable)([HEXColor(0x000000, 0.4) CGColor]);
        colorAnimation.toValue = (__bridge id _Nullable)([HEXColor(0x000000, 0) CGColor]);
    }
    
    
    colorAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    colorAnimation.fillMode = kCAFillModeForwards;
    colorAnimation.removedOnCompletion = NO;
    [self.layer addAnimation:colorAnimation forKey:@"colorAnimation"];
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (self.dataBlock) {
        self.dataBlock([NSString stringWithFormat:@"%ld",(long)indexPath.row+1]);
    }
    [self dismissAlertView];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    if (_sec) {
        return;
    }
    [self dismissAlertView];
}
- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
}
@end
